package com.lagou.utils;

import java.sql.Connection;
import java.sql.SQLException;

public class ConnectionUtils {

    private ThreadLocal<Connection> threadLocal= new ThreadLocal<>();

    private ConnectionUtils() {}

    private static ConnectionUtils connectionUtils=new ConnectionUtils();
    public static ConnectionUtils getInstance(){return  connectionUtils;}

    /**
     * 从当前线程获得连接
     */

    public Connection getCurrentConnection() throws SQLException {
        Connection connection = threadLocal.get();
        if (null == connection){
            connection =  DruidUtils.getInstance().getConnection();
            threadLocal.set(connection);
        }
        return connection;
    }
}
